﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Codebricks.Db.Interface;
using Codebricks.Db.Attributes;
using Codebricks.Db;
using NLog;

namespace KBVault.DAL.Entities
{
    [MySqlTable("settings")]
    public class Settings : IMysqlTable
    {
        [MySqlPrimaryKey]
        [MySqlField("Id")]
        public int Id { get; set; }

        [MySqlField("Art_Cnt_Per_Cat_On_Home")]
        public int ArticleCountPerCategoryOnHomePage { get; set; }

        [MySqlField("Company_Name")]
        public string CompanyName { get; set; }

        [MySqlField("Tagline")]
        public string TagLine { get; set; }

        [MySqlField("Jumbotron_Text")]
        public string JumbotronText { get; set; }

        [MySqlField("ShareThis_Pub_Key")]
        public string ShareThisPublisherKey { get; set; }

        [MySqlField("DisqusShortName")]
        public string DisqusShortName { get; set; }

        private static Settings instance;
        
        public static void Clear()
        {
            instance = null;
        }

        public static void Save()
        {
            try
            {
                MySqlDb.Update(instance);
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex.Message);
                throw;
            }
        }
        public static Settings Instance
        {
            get
            {
                if (instance == null)
                {
                    try
                    {
                        Settings s = new Settings() { Id = 1 };
                        instance = MySqlDb.Get<Settings>(s);
                        if (instance == null)
                        {
                            instance = s;
                            MySqlDb.Insert(instance);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.GetCurrentClassLogger().Error(ex.Message);
                        throw;
                    }
                }
                return instance;
            }
        }
    }
}
